//
//  PlanteNumberChangeViewController.m
//  ICrEAM
//
//  Created by Leona on 2017/4/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "PlanteNumberChangeViewController.h"
#import "MenuViewController.h"
#import "MoreCarInfoView.h"
#import "ICMPersonerModel.h"
#import "ICMCarModel.h"
#import "ChangeLisenceAction.h"
@interface PlanteNumberChangeViewController ()<UITextViewDelegate>
@property(nonatomic,strong)UIPopoverController *popCtr;
@property(nonatomic,strong)MoreCarInfoView *moreCarInfoView;
@property(nonatomic,strong)ICMPersonerModel *personModel;
@property(nonatomic,strong)ICMPersonerModel *selectedpersonModel;
@property(nonatomic,strong)ICMCarModel *carModel;
@property(nonatomic,strong)ICMCarModel *selectedcarModel;
@property (weak, nonatomic) IBOutlet UILabel *originalCarLabel;
@property (weak, nonatomic) IBOutlet UILabel *neoCarLabel;
@property (weak, nonatomic) IBOutlet UIButton *changeButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
//原
@property (weak, nonatomic) IBOutlet UILabel *originalVinLabel;
@property (weak, nonatomic) IBOutlet UILabel *originalCarNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *originalOwnerNumberLaber;
@property (weak, nonatomic) IBOutlet UILabel *originalOwnerNameLaber;
@property (weak, nonatomic) IBOutlet UILabel *originalOwnerPhoneLabel;

//新
@property (weak, nonatomic) IBOutlet UILabel *neoVinLabel;
@property (weak, nonatomic) IBOutlet UILabel *neoCarNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *neoOwnerNumberLaber;
@property (weak, nonatomic) IBOutlet UILabel *neoOwnerNameLaber;
@property (weak, nonatomic) IBOutlet UILabel *neoOwnerPhoneLabel;

//空白view

@end

@implementation PlanteNumberChangeViewController
- (MoreCarInfoView *)moreCarInfoView
{
    if(!_moreCarInfoView)
    {
        _moreCarInfoView = [[MoreCarInfoView alloc] init];
    }
    return _moreCarInfoView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _originalVinLabel.textColor = TitleColor;
    _originalCarNumberLabel.textColor = TitleColor;
    _originalOwnerNumberLaber.textColor = TitleColor;
    _originalOwnerNameLaber.textColor = TitleColor;
    _originalOwnerPhoneLabel.textColor = TitleColor;
    
    _neoVinLabel.textColor = TitleColor;
    _neoCarNumberLabel.textColor = TitleColor;
    _neoOwnerNumberLaber.textColor = TitleColor;
    _neoOwnerNameLaber.textColor = TitleColor;
    _neoOwnerPhoneLabel.textColor = TitleColor;
    
    _originalCarLabel.textColor = TitleColor;
    _neoCarLabel.textColor = TitleColor;
    [_changeButton setBackgroundColor:TitleColor];
    [_cancelButton setBackgroundColor:TitleColor];
    
    
    if (self.carArray&&self.carArray.count==1) {
        
        self.selectedpersonModel=[ICMPersonerModel yy_modelWithDictionary:self.ownerArray[0]];
        self.selectedcarModel=[ICMCarModel yy_modelWithDictionary:self.carArray[0]];
        [self setUpViewWithCarModel: self.selectedcarModel personModel:self.selectedpersonModel];
        self.chooseCarNumberButton.hidden=YES;
    }else if(self.carArray.count>1)
        
    {
        self.chooseCarNumberButton.hidden=NO;
        [self popPopView];
    }else{
        
        self.chooseCarNumberButton.hidden=NO;
        self.neoCarNumberTextField.text=self.linsence;
    }
    [self requestVin:self.vin];

    //添加的是背景view，并不是自身的view
    [self.blankView addSubview:self.view];
    
    
    
}
-(void)requestVin:(NSString*)vin{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [parameters setObject:@"" forKey:@"LICENSE"];
    [parameters setObject:vin forKey:@"VIN"];
    
    [manager startGetRequestWithURL:QueryOwnerByVinLicense parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            
            if ([data[@"DATA_STATUS"] integerValue] == 0){
                
                return ;
            }else{
                
                NSArray *carModelArray = data[@"TM_VEHICLE"];
                if (![carModelArray isKindOfClass:[NSArray class]]) {
                    return ;
                }
                
                
                NSArray *personerModelArray = data[@"TM_OWNER"];
                if (![personerModelArray isKindOfClass:[NSArray class]]){
                    return;
                }
                self.personModel=[ICMPersonerModel yy_modelWithDictionary:personerModelArray[0]];
                self.carModel=[ICMCarModel yy_modelWithDictionary:carModelArray[0]];
                self.originalVINtextField.text=self.carModel.vinNum;
                self.originalOwnerIdTextField.text=self.personModel.OWNER_NO;
                self.originalCarNumberTextField.text=self.carModel.carNum;
                self.originalOwnerNameTextField.text=self.personModel.ownerName;
                self.originalOwnerPhoneNumberTextField.text=self.personModel.phoneNum;
                
                [self setRedTextColor];

  

            }
        }
    }];
}
-(void)setUpViewWithCarModel:(ICMCarModel *)carModel personModel:(ICMPersonerModel*)person{
    self.selectedcarModel=carModel;
    self.selectedpersonModel=person;
    self.neoVINtextField.text=carModel.vinNum;
    self.neoOwnerIdTextField.text=person.OWNER_NO;
    self.neoCarNumberTextField.text=carModel.carNum;
    self.neoOwnerNameTextField.text=person.ownerName;
    self.neoOwnerPhoneNumberTextField.text=person.phoneNum;
    
    
    
    
    
    
    
}
- (void)popPopView {
    self.moreCarInfoView.carArray = [[NSArray alloc] initWithArray:self.carArray];
    self.moreCarInfoView.personArray = [[NSArray alloc] initWithArray:self.ownerArray];
    
    self.moreCarInfoView.delegate = self;
    CGRect rect=[self.neoCarNumberTextField.superview convertRect:self.neoCarNumberTextField.frame toView:self.view];
    //frame
    if(self.carArray.count>=6)
    {
        
        [self.moreCarInfoView resetViewFrame:CGRectMake(rect.origin.x, rect.origin.y + rect.size.height, 400, 44*6)];
    }
    else
    {
        [self.moreCarInfoView resetViewFrame:CGRectMake(rect.origin.x, rect.origin.y + rect.size.height, 400, 44*self.carArray.count)];
    }
    
    
    [self.view addSubview:self.moreCarInfoView];
    /*
     // 设置内容控制器
     MenuViewController *menuVC = [[MenuViewController alloc] init];
     self.popCtr = [[UIPopoverController alloc] initWithContentViewController:menuVC];
     
     // 设置内容的尺寸  如果内容控制器里设置了 preferredContentSize 大小 self.preferredContentSize = CGSizeMake(200, 200); 那么 self.popOver.popoverContentSize 将无意义，而且在实际开发中也应该由内容控制器控制大小
     self.popCtr.popoverContentSize = CGSizeMake(self.neoCarNumberTextField.frame.size.width, 100);
     
     // 从那个地方显示出来 第一个参数是popOver出现的位置，第二个参数是箭头方向
     //    [self.popCtr presentPopoverFromBarButtonItem:self.chooseCarNumberButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     [self.popCtr presentPopoverFromRect:self.chooseCarNumberButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
     // 这个方法是自定义出现的位置  Rect：箭头指向的区域范围，以View的左上角为坐标原点
     //    [self.popOver presentPopoverFromRect:CGRectMake(GetWindowWidth * 0.5, 200, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
     */
    
}

- (void)didClickedCellAtIndex:(NSInteger)index WithVIN:(NSString *)vin{
    ICMPersonerModel *person=[ICMPersonerModel yy_modelWithDictionary:self.ownerArray[index]];
    ICMCarModel *carModel=[ICMCarModel yy_modelWithDictionary:self.carArray[index]];
    [self setUpViewWithCarModel:carModel personModel:person];
    [self.moreCarInfoView removeFromSuperview];
}


- (IBAction)changeAction:(UIButton *)sender {
    if (![self.personModel.OWNER_NO isEqualToString:self.selectedpersonModel.OWNER_NO]&&!IsStrEmpty(self.selectedpersonModel.OWNER_NO)) {
        [self showText:@"两辆车的车主不一致，不允许变更车牌！"];
        return;
    }
    ChangeLisenceAction *action=[[ChangeLisenceAction alloc]init];
    
    action.OLD_VIN=self.carModel.vinNum;
    action.OLD_LICENSE=self.carModel.carNum;
    action.NEW_VIN=self.selectedcarModel.vinNum?self.selectedcarModel.vinNum:@"";
    action.NEW_LICENSE=self.selectedcarModel.carNum?self.selectedcarModel.carNum:self.neoCarNumberTextField.text;
    [self requestDataWithAction:action haveHud:YES showError:YES success:^(__kindof YTKBaseRequest *request) {
        if (self.sureBlock) {
            
            //           self.sureBlock();
            [self removeAdImageView];
        }
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
    
}
- (IBAction)cancelChangeAction:(UIButton *)sender {
    [self removeAdImageView];
}
- (IBAction)cancelUpdataAction:(UIButton *)sender {
}
- (IBAction)chooseCarNumberAction:(UIButton *)sender {
    
    [self popPopView];
    
    
}

- (void)showOnView:(UIWindow *)window
{
    CGRect frame = [UIScreen mainScreen].bounds;
    
    //    CGFloat height=with*
    //        self.view.frame = CGRectMake(ScreenWidth-600, 200, 550, 450);
    self.view.frame=frame;
    [window addSubview:self.view];
    if (!IOS7_OR_LATER) {
        frame.origin.y = 20;
        frame.size.height-=20;
    }
    
}
- (void)setDismissBlock:(ICrEAMBlock)block
{
    if (block != dismissBlock) {
        dismissBlock = [block copy];
    }
}
//移除页面
- (void)removeAdImageView
{
    [UIView animateWithDuration:3.0f animations:^{
        //        self.bgView.alpha = 0.999f;
    } completion:^(BOOL finished) {
        //        [self.bgView removeFromSuperview];
        
        [self.blankView removeFromSuperview];
        if (dismissBlock) {
            dismissBlock();
        }
    }];
    
}
- (void)animationFinished{
    [self removeAdImageView];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    /*
     self.view.backgroundColor=[UIColor clearColor];
     CGContextRef contentext = UIGraphicsGetCurrentContext();
     
     [UIView beginAnimations:nil context:contentext];
     
     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
     
     [UIView setAnimationDuration:0.7];
     
     self.view.transform = CGAffineTransformMakeTranslation(self.view.width, 0);
     
     [UIView setAnimationDelegate:self];
     
     [UIView setAnimationDidStopSelector:@selector(animationFinished) ];
     
     [UIView commitAnimations];
     */
}
- (UIView *)blankView{
    if (!_blankView) {
        _blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _blankView.backgroundColor = [UIColor colorWithHex:BorderColor_Gray_Hex andAlpha:0.8];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAllView)];
        [_blankView addGestureRecognizer:tap];
        
    }
    return _blankView;
}
-(void)hideAllView{
    [self.blankView removeFromSuperview];
}

-(void)setRedTextColor{
    //不同的高亮
    if(![_originalVINtextField.text isEqualToString:_neoVINtextField.text]){
        
        _neoVINtextField.textColor = [UIColor redColor];
        
    }
    if(![_originalCarNumberTextField.text isEqualToString:_neoCarNumberTextField.text]){
        
        _neoCarNumberTextField.textColor = [UIColor redColor];
        
    }
    
    if(![_originalVINtextField.text isEqualToString:_neoVINtextField.text]){
        
        _neoVINtextField.textColor = [UIColor redColor];
        
    }
    if(![_originalOwnerIdTextField.text isEqualToString:_neoOwnerIdTextField.text]){
        
        _neoOwnerIdTextField.textColor = [UIColor redColor];
        
    }
    if(![_originalOwnerNameTextField.text isEqualToString:_neoOwnerNameTextField.text]){
        
        _neoOwnerNameTextField.textColor = [UIColor redColor];
        
    }
    if(![_originalOwnerPhoneNumberTextField.text isEqualToString:_neoOwnerPhoneNumberTextField.text]){
        
        _neoOwnerPhoneNumberTextField.textColor = [UIColor redColor];
        
    }


}


@end
